package com.javalong.affairs.module.recycle.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.javalong.affairs.R;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.MapKey;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.SimpleMessage;
import com.javalong.affairs.module.recycle.activity.MsgRecycleDetailActivity;
import com.javalong.affairs.module.recycle.adapter.RecycleAdapter;
import com.javalong.affairs.base.ui.SimplePullRefreshList;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;

public class ReceiveRecycleFragment extends BaseFragment implements
		OnItemClickListener {
	private SimplePullRefreshList lv_receiver;
	private RecycleAdapter ad_notification;
	private List<SimpleMessage> msgList;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_notificationreceive,
				container, false);
		lv_receiver = (SimplePullRefreshList) view
				.findViewById(R.id.lv_receiver);
		msgList = AllData.GetInstance().appendListData(
				R.string.data_recycle_receive, new ArrayList<SimpleMessage>());
		ad_notification = new RecycleAdapter(
				getActivity(),
				msgList,
				com.javalong.affairs.module.recycle.adapter.RecycleAdapter.Type.Receive);
		lv_receiver.setAdapter(ad_notification);
		lv_receiver.initData(getMapData());
		lv_receiver.refresh();
		lv_receiver.setOnItemClickListener(this);
		return view;
	}

	private Map<String, Object> getMapData() {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(MapKey.ACTION_NAME, "SendMessage");
		param.put(MapKey.METHOD_NAME, "recycleBin");
		param.put(MapKey.DATA_RESID, R.string.data_recycle_receive);
		param.put(MapKey.DATA_TYPE, DataType.LIST_SIMPLEMESSAGE);
		return param;
	}

	@Override
	public void refresh() {
		super.refresh();
		lv_receiver.refresh();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (position < 2) {
			return;
		}
		if (msgList != null && msgList.size() > position - 2) {
			SimpleMessage msgInfo = msgList.get(position - 2);
			Bundle bundle = new Bundle();
			bundle.putString(BundleKey.MSGID, msgInfo.messageId);
			bundle.putString(BundleKey.SIMPLEMSGID, msgInfo.id);
			IntentManager.doActivity(getActivity(),
					MsgRecycleDetailActivity.class, bundle);
		}
	}

	private void delete(SimpleMessage simpleMsg) {
		String url = UrlUtils.getUrl("SendMessage", "delete");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, simpleMsg.id);
		HttpUtils.post(url, params, new BaseResponseListener(null) {
			@Override
			public void onSuccess(Object obj) {
				super.onSuccess(obj);
				ToastUtils.showMessage("删除成功");
			}
		});
	}
}
